﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class ManageComplain : BasePage
    {
        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            var list = from c in contex.Complaints orderby c.update_date descending select c;
            var query = list.AsQueryable();
            if (this.txtLoginName.Text.Trim() != "")
            {
                query = query.Where(p => p.supporter.supporter_name.Contains(this.txtLoginName.Text.Trim()) || p.supporter.enterprise_name.Contains(this.txtLoginName.Text.Trim()));
            }
            if (this.ddlValidate.SelectedItem.Value != "")
            {
                char activeFlag = char.Parse(this.ddlValidate.SelectedItem.Value);
                query = query.Where(p => p.record_status == activeFlag);
            }

            List<Complaint> listSup = query.ToList();
            AspNetPager1.RecordCount = listSup.Count;

            psd.DataSource = listSup;
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptCategory.DataSource = psd;
            this.rptCategory.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                Complaint cat = item as Complaint;

                Label recordNo = e.Item.FindControl("lblNo") as Label;
                Literal lblCreater = e.Item.FindControl("lblCreater") as Literal;
                Label lblStatus = e.Item.FindControl("lblStatus") as Label;
                LinkButton button = e.Item.FindControl("btnDelete") as LinkButton;
                CheckBox chkDisplay = e.Item.FindControl("chkDisplay") as CheckBox;
                lblCreater.Text = "<a href='userDetailsReadonly.aspx?id=" + cat.supporter_id + "'>";
                lblCreater.Text += cat.supporter.supporter_type == 'I' ? cat.supporter.supporter_name : cat.supporter.enterprise_name+"</a>";
                if (cat.display_toppage.HasValue)
                {
                    if (cat.display_toppage.Value == '0')
                    {
                        chkDisplay.Checked = false;
                    }
                    else
                    {
                        chkDisplay.Checked = true;
                    }
                }
                else
                {
                    chkDisplay.Checked = false;
                }
                lblStatus.Text = GetStatus(cat.record_status.ToString());
                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                button.Attributes.Add("onclick", "return confirm('您确定要删除吗？删除后该分类下面的文章也将被删除');");
            }

        }

        private string GetStatus(string code)
        {
            string status = "";
            switch (code)
            {
                case "N": status = "已提交"; break;
                case "R": status = "已受理"; break;
                case "P": status = "已处理"; break;
            }
            return status;
        }

        protected void rptCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
                int category_id = Convert.ToInt32(e.CommandArgument);
                Complaint cat = contex.Complaints.SingleOrDefault(p => p.ID == category_id);
                if (cat != null)
                {
                    contex.Complaints.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                }
                BindGrid();
            }
            if (e.CommandName == "Display")
            {
                int category_id = Convert.ToInt32(e.CommandArgument);
                Complaint cat = contex.Complaints.SingleOrDefault(p => p.ID == category_id);
                if (cat != null)
                {
                    if (cat.display_toppage.HasValue)
                    {
                        if (cat.display_toppage.Value == '0')
                        {
                            cat.display_toppage = '1';
                        }
                        else
                        {
                            cat.display_toppage = '0';
                        }
                    }
                    else
                    {
                        cat.display_toppage = '1';
                    }
                    contex.SubmitChanges();
                }
                BindGrid();
            }
        }
    }
}